package uestc.lj.gulishop.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import uestc.lj.gulishop.order.vo.PayVo;

/**
 * @Author:Crazlee
 * @Date:2021/6/22
 */
@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private String app_id = "2021000117676557";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private String merchant_private_key = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCCQDICQvZjJhti5OMw5mUj/x1T/Lm4z9KvgN+jATsdoppPXyKmCtynYCyxXW7gDWk0y/BK1gxv1Wp9aU5XHUfCMfOkzgPkcmgfSCtwql/Xwi82B5QqK4Scj9j1VEJyHF3F8BJb+/cNsuUcMx0FyDJBGFOSuZUQS4d9+hr0YIpOHl81vNenZXXAmOb7Th6lXE23QK3WTb4UJv0cSagbvtmRFFSMoQD2H6ZLH7QoQfxzr0OAp9ctQ+BLqoWzJ0spMX/wOFqvouvBiCz8MFwJl3Yw6PkaH3M6xPmkRiEEv9Rk3gEbu7BUVtWylG4LjnoB0pTh7rWLxcVgRkxYDPXiOqmrAgMBAAECggEAf3rzNzFw0wPGBBm/numwjfWwL7aH07FcJLe+qlyXDl9KJmsXj2nMIulHdOTckt+tW6ZK7gPnskEv88TwjuSyige3zlJwjtPGwu8wzRmDqrl82aW7RzbGmTFVCHBKhELtKMDaRzspxzeTc/E286TIQtvbWBzZH1S9LPhwD0S5uNApRAs85D0bYVOCwWOjD3wdNsEARFbkdMxvKa4epT1dGWSfVi80Uf6Vxu/guB/SL3VHMbIHiBat1ysuZA+9x/xxx4Y85rjMbx4lVNtEPjufKorLyIey4kuHuXQqWr/g30wHCCxRbZ8Zlg10JNx9lg3+MbAlhZFiDxQuGIxCewKpyQKBgQDVqqZkScXDFxRBdGC2oDCAbPD3Y50yopCQq/DjRL9FOVRRSFaO5qWNMYIyixShhor6g3zRmwSfm6SqOQDNE/ZDccPU5SyWUS4hczOU2M146WuiEUZ8Q7fziCcg5d9d+wUEt/RqBmeF6poMVpTFICx7VtqSaMt7W7XPx9RAQzpg7wKBgQCcDp+wMiYikgXo0S4NVZc5HSfJKzd4164PFkRTRavJC8XeZuMohyhs6imkfvuDaCIRRQCt8U5LnJ4/ISgcCgHv8O5qo8MMucX7akUIQVUlFOgi1HRkSvQoytvuXFKUwciUy+JoWDjCOpTykOXB4diYDcsRU1iTcaTK4ad2PduLBQKBgQCVsAT783b5svx80qRCrTzta+Tw27ewVpVC1UJgZbQ+MeixPSVu3YUTwqB8+lTMyG4rDNzwF1MI4khc/Kxj/90h6NySFK/E60CPLJ7xY2WnlJfUJZgkzGhd03/eG0z+ETK12I4BjVUPS4j6pdl5tI2VJwbfVU1gsaJQ26UGFFMcvwKBgQCBOcbYQ566j8qe8OC+n/qai98PQV8tDQSQb7LiddX7pGkUcoR2Yf2CuyECmzgwp4FWZZpbb2+TQzzzL8Sj+1GBXDRBUBxLJGaie2xwmpEkJLzZYq6DAmJOiTU3m3GyxwkO1gpEt5FAnPwMJRpsw1T1DK70SN0XM0rvyY4/Zpeu9QKBgCleh0CqJHHL7jAIi2m3OpYZ4SYjUhDfd/lNw1DWMN96b927UBEbhAZZMIlxQSg+qBYWPpMWdAujq5gc2CWZWbxnySTxE3yWWFLJnbG6s9G/K7meUuG1PjHRvZhYE+s9z68OJkfuQB0qoXz/o1zJ5U+hf3VsljoyzA3mj2AEO8UY";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuckP6WHV4dDB0JmGUrJe6mf+lb11EJ+1lBCNXzpTztn9nzBFOQIj0tE1Gps1GEjJIxKVQJB5mPREkW/eme3NF9ZMF0zQVBboZYk4Ngt43eosE1qa8v6DtSeHSHvHs1ugnAbr5c6rIgUQjT9bjtu/cwVwGt1guvTZ17ud37haOeAxZTzZn21aHm0hKPHL8ngKPLhFaNOZQYysFPu26B3ylDqJwJ1J09cx0WeK2MvzyQxN2C+iXpWZmHtAVuPMXzaOzkh6uR8iZN7nE8uIsOe9Csbl2qG+wYvx+Z1zsnllf0iVAVeyqGr2UbtQkl5dJnwx/xbkmA3ezBoyCmS2lgeY4wIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private String notify_url = "http://406329do19.qicp.vip/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private String return_url = "http://member.gulishop.com/memberOrder.html";

    // 签名方式
    private String sign_type = "RSA2";

    // 字符编码格式
    private String charset = "UTF-8";

    // 超时时间
    private String timeout = "30m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\","
                + "\"total_amount\":\"" + total_amount + "\","
                + "\"subject\":\"" + subject + "\","
                + "\"body\":\"" + body + "\","
                + "\"timeout_express\":\"" + timeout + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应：" + result);

        return result;

    }
}


